package service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import dto.OrderNotifyDto;
import enums.OrderType;
import jms.ComplaintSender;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pojo.Complaint;
import pojo.Customer;
import pojo.Merchant;
import pojo.Order;
import pojo.OrderItem;
import service.OrderManager;
import util.JerseyClient;
import util.JsonUtil;
import dao.CustomerDao;
import dao.OrderDao;
import dto.ComplaintDto;

/**
 * Created by CHENCO7 on 8/12/2017.
 */
@Service(value = "orderManager")
public class OrderManagerImpl implements OrderManager {
	@Resource(name = "complaintSender")
	private ComplaintSender complainSender;
	
    @Resource(name = "orderDaoImpl")
    private OrderDao orderDao;

    @Resource(name = "customerDao")
    private CustomerDao customerDao;

	@Override
    public List<OrderItem> addOrderItemToSession(OrderItem item, List<OrderItem> orderList) {
        if(orderList == null){
            orderList = new ArrayList<OrderItem>();
        }

        boolean addOrderFlag = true;
        for(int i = 0; i < orderList.size(); i++){
            if (orderList.get(i).getName().equals(item.getName())){
                orderList.get(i).setAmount(item.getAmount());
                orderList.get(i).setPrice(item.getPrice());
                addOrderFlag = false;
            }
        }
        if (addOrderFlag){
            orderList.add(item);
        }

        return orderList;
    }

    @Override
    @Transactional
    public boolean addOrder(Order order) {
        Customer customer = customerDao.loadCustomer(order.getCustomer().getId());
        order.setCustomer(customer);
        if(orderDao.addOrder(order) != null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Order loadOrder(String id) {
        return orderDao.loadOrder(id);
    }

    @Override
    public Order updateOrder(Order order) {
        return orderDao.updateOrder(order);
    }

    @Override
    public List<Order> loadOrderList(String id) {
        List<Order> list = orderDao.getOrderListByCustomerId(id);
        List<Order> voList = new ArrayList<Order>();
        for (Order order : list){
            Order vo = new Order();
            vo.setId(order.getId());
            Customer customer = new Customer();
            customer.setName(order.getCustomer().getName());
            Merchant merchant = new Merchant();
            if(order.getMerchant()!=null){
                merchant.setName(order.getMerchant().getName());
                vo.setMerchant(merchant);
            }
            vo.setOrderItemList(order.getOrderItemList());
            vo.setCustomer(customer);
            vo.setProcessNumber(order.getProcessNumber());
            vo.setStatus(order.getStatus());
            vo.setCreateDate(order.getCreateDate());
            vo.setDeliveryAddress(order.getDeliveryAddress());
            voList.add(vo);
        }
        System.out.println("...");
        return voList;
    }

	@Override
	@Transactional
	public boolean addComment(String comment, String starCount, String o_id) {
        if(orderDao.addComment(comment, starCount, o_id) != null){
            return true;
        }else {
            return false;
        }
	}
	
	@Override
	@Transactional
	public boolean addComplain(String complain, String o_id) {
        if(orderDao.addComplain(complain, o_id) != null){
        	Order order = orderDao.loadOrder(o_id);
        	Merchant merchant = order.getMerchant();
        	Customer customer = order.getCustomer();
        	//发送jms给A端
        	System.out.println(customer + " " + merchant);
        	ComplaintDto complain1 = new ComplaintDto(customer.getName(), complain, new Date(), merchant.getName());
        	String sendMessage = JsonUtil.toJsonString(complain1);
        	System.out.println(sendMessage + " in add complain...");
        	complainSender.send(sendMessage);
            return true;
        }else {
            return false;
        }
	}

    @Override
    public Order loadOrderItem(String orderId) {
        Order order = orderDao.loadOrderItem(orderId);
        return order;
    }

    @Override
    @Transactional
    public Order determineReceipt(String orderId) {
        Order order = orderDao.loadOrder(orderId);
        order.setStatus(OrderType.SUCCESS.toString());
        return order;
    }
}
